package com.zcy.pmp.controller;

import com.zcy.basic.annotation.PermissionApi;
import com.zcy.basic.data.ContextData;
import com.zcy.basic.util.AjaxResult;
import com.zcy.basic.util.EasyPoiUtil;
import com.zcy.basic.util.PageInfo;
import com.zcy.basic.util.PageList;
import com.zcy.pmp.domain.Employee;
import com.zcy.pmp.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * <p>
 *  员工管理
 * </p>
 *
 * @author lucker
 * @date 2023/11/28 19:24
 */
@RestController
@RequestMapping("/employee")
@Api(value = "员工相关API", description = "员工相关增删改查")
@PermissionApi(name = "员工管理", description = "员工相关增删改查")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @ApiOperation(value = "新增/修改员工")
    @PermissionApi(name = "新增/修改员工")
    @PutMapping
    public AjaxResult saveEmployee(@RequestBody Employee employee) {
        try {
            if (employee.getId() != null) {
                employeeService.update(employee);
            } else {
                employeeService.add(employee);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "根据ID删除员工")
    @PermissionApi(name = "根据ID删除员工")
    @DeleteMapping("/{id}")
    public AjaxResult deleteEmployeeById(@PathVariable("id") Long id) {
        try {
            employeeService.deleteById(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "根据ID查询员工")
    @PermissionApi(name = "根据ID查询员工")
    @GetMapping("/{id}")
    public AjaxResult queryEmployeeById(@PathVariable("id") Long id) {
        try {
            Employee employee = employeeService.queryById(id);
            return AjaxResult.success(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "查询所有员工信息")
    @PermissionApi(name = "查询所有员工信息")
    @GetMapping
    public AjaxResult listAllEmployee() {
        try {
            List<Employee> employees = employeeService.listAll();
            return AjaxResult.success(employees);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @PostMapping
    @ApiOperation("分页高级查询")
    @PermissionApi(name = "员工分页高级查询")
    public AjaxResult queryEmployeePage(@RequestBody PageInfo pageInfo) {
        try {
            PageList<Employee> employeePageList = employeeService.queryPage(pageInfo);
            return AjaxResult.success(employeePageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     * 批量删除
     * @param ids 员工ID数组
     * @return AjaxResult
     */
    @PatchMapping
    @ApiOperation("批量删除")
    @PermissionApi(name = "批量删除员工")
    public AjaxResult deleteBatchByEmployeeIds(@ApiParam("员工ID数组") @RequestBody List<Long> ids) {
        try {
            employeeService.deleteBatch(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    @GetMapping("/export")
    public void export(@RequestParam(name = "keyword",required = false) String keyword,HttpServletResponse response) {
        List<Employee> employees = employeeService.queryList(keyword);
        employeeService.export(employees, response, "员工");

    }

    @PostMapping("/import")
    @ApiOperation("导入员工")
    public AjaxResult importEmployees(@RequestBody MultipartFile file) {
        String token = "123";
        List<Employee> errorEmployees = employeeService.importEmployees(token, file);
        if (errorEmployees != null && !errorEmployees.isEmpty()) {
            return AjaxResult.error(errorEmployees, "部分数据有误");
        }
        return AjaxResult.success("员工导入成功");
    }

    @GetMapping("/error")
    public void getErrorExcel(HttpServletResponse response) {
        String token = "123";
//        List<Employee> employees = ContextData.ERROR_EXCEL.get(token);
        List<Employee> employees = ContextData.ERROR_EXCEL;
        if (employees == null) {
            return;
        }
//        employees.forEach(System.out::println);
//        List<Employee> employeesList = employeeService.queryList("");

        List<Employee> employees1 = new ArrayList<>();

        for (Employee employee : employees) {
            employees1.add(
                    new Employee()
                            .setUsername(employee.getUsername())
                            .setPassword(employee.getPassword())
                            .setEmail(employee.getEmail())
                            .setRole(employee.getRole())
                            .setAge(employee.getAge())
                            .setDepartment(employee.getDepartment())
                            .setHeadImage(employee.getHeadImage())
                            .setId(employee.getId()));
        }

        employeeService.export(employees1, response, "员工错误数据");
    }

}
